package com.superb.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * spring上下文获取，ioc监听池
 * @Author: ajie
 * @CreateTime: 2024-05-15 15:07
 */
@Lazy(value = false)
@Component("springContextUtils")
public class SpringContextUtils implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext context = null;

    public static ApplicationContext getContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }


    /**
     * 获取当前环境
     *
     * @return
     */
    public static String getActiveProfile() {
        String[] activeProfiles = context.getEnvironment().getActiveProfiles();
        if (activeProfiles.length == 0) {
            return "dev";
        }
        return activeProfiles[0];
    }

    /**
     * 获取当前环境相关配置
     *
     * @param key
     * @return
     */
    public static String getEnvironmentProperty(String key) {
        return getEnvironmentProperty(key, String.class);
    }

    /**
     * 获取当前环境相关配置
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getEnvironmentProperty(String key, String defaultValue) {
        return getEnvironmentProperty(key, String.class, defaultValue);
    }

    /**
     * 获取当前环境相关配置
     *
     * @param key
     * @param targetType
     * @param <T>
     * @return
     */
    public static <T> T getEnvironmentProperty(String key, Class<T> targetType) {
        return context.getEnvironment().getProperty(key, targetType);
    }

    /**
     * 获取当前环境相关配置
     *
     * @param key
     * @param targetType
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static <T> T getEnvironmentProperty(String key, Class<T> targetType, T defaultValue) {
        return context.getEnvironment().getProperty(key, targetType, defaultValue);
    }

    /**
     * 获取当前环境相关配置对象
     *
     * @param key
     * @param targetType
     * @param <T>
     * @return
     */
    public static <T> T getEnvironmentPropertyObj(String key, Class<T> targetType) {
        return Binder.get(context.getEnvironment()).bind(key, Bindable.of(targetType)).get();
    }

    /**
     * 获取当前环境相关配置Set
     *
     * @param key
     * @param targetType
     * @param <T>
     * @return
     */
    public static <T> Set<T> getEnvironmentPropertySet(String key, Class<T> targetType) {
        return Binder.get(context.getEnvironment()).bind(key, Bindable.setOf(targetType)).get();
    }

    /**
     * 获取当前环境相关配置List
     *
     * @param key
     * @param targetType
     * @param <T>
     * @return
     */
    public static <T> List<T> getEnvironmentPropertyList(String key, Class<T> targetType) {
        return Binder.get(context.getEnvironment()).bind(key, Bindable.listOf(targetType)).get();
    }

    /**
     * 获取当前环境相关配置Map
     *
     * @param key
     * @param keyType
     * @param valueType
     * @param <T>
     * @param <M>
     * @return
     */
    public static <T, M> Map<T, M> getEnvironmentPropertyMap(String key, Class<T> keyType, Class<M> valueType) {
        return Binder.get(context.getEnvironment()).bind(key, Bindable.mapOf(keyType, valueType)).get();
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static <T> T getBean(String name) {
        return (T) context.getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }

    public static <T> List<T> getBeans(Class<T> clazz) {
        return new ArrayList<>(context.getBeansOfType(clazz).values());
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return context.getBean(name, clazz);
    }


    /**
     * 发布事件
     *
     * @param event
     */
    public static void publishEvent(ApplicationEvent event) {
        if (context == null) {
            return;
        }
        context.publishEvent(event);
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    public static void clearHolder() {
        context = null;
    }

    @Override
    public void destroy() {
        SpringContextUtils.clearHolder();
    }
}
